/*
 * 后台管理模板
 */
/**
 * 扩展一个Menu模块，动态获取左侧菜单
 * 目前多级子菜单是通过延时循环获取的方式
 * 也可以采用点击时获取下级子菜单的方式
 * 当然，具体的情况要参照自己的需求及api返回结构
 * 现在只支持一级菜单,如果需要二级菜单,可修改数据库和此处代码
 **/
var $ = layui.jquery;
$(function(){
	var menuList=null;
	var subItems = [];
	new APIRequest(
		{
			url: global.api_url + "/sysPermission/getPermitById",
			header: {content_type: "application/json;charset=UTF-8",Authorization:getUser().token},
			method: "POST",
			async: false,
			success: function (result) {
				menuList = result.data;
			},
			dataType: "json"
		}
	);
	layui.define('jquery', function(exports) {
		var $ = layui.jquery;
		var menu = {
			getMenu: function() {
				var that = this;
				if (!$('#side-nav').length) {
					return;
				}
				 var items = [];
				 var _ul = $('<ul></ul>').addClass('nav').attr('id', 'nav');
				 $.each(menuList, function(index, val) {
					 if (val.pid==0 && val.istype==0) {
						 var item = '<li id="menu' + val.id + '"><a _href="' + val.page + '"><i class="iconfont">' + val.icon +
							 '</i><cite>' + val.name +
							 '</cite><i class="iconfont nav_right">&#xe697;</i></a></li>';
						 items.push(item);
						 var subList = getParentArry(val.id, menuList);
						 if (subList.length > 0) {
							 //有二级子菜单
							 setTimeout(function () {
								 that.getSubMenu(val.id, subList);
							 }, 500);
						 }
					 };
				 });
				 $('#side-nav').empty();
				 _ul.append(items.join(''));
				 $('#side-nav').append(_ul);

			},
			/**
			 * @param {Object} id 上级子菜单id,拼接menu获取元素
			 * @param {Object} subList 子菜单列表
			 */
			getSubMenu: function(id, subList) {
				var that = this;
				var subItems = [];
				var subUl = $('<ul></ul>').addClass('sub-menu');
				$.each(subList, function(idx, sub) {
					var subItem = '<li id="menu' + sub.id + '"><a _href="' + sub.page + '"><i class="iconfont">&#xe6a7;</i><cite>' +
						sub.name + '</cite></a></li>';
					subItems.push(subItem);
				/*	if (sub.children && sub.children.length) {
						setTimeout(function() {
							that.getSubMenu(sub.id, sub.children);
						}, 1500);
					}*/
				});
				subUl.append(subItems.join(''));
				$('#menu' + id).append(subUl);
			}
		};

		exports('menu', menu);
	});
})
//根据菜单主键id获取下级菜单
//id：菜单主键id
//arry：菜单数组信息
function getParentArry(id, arry) {
	var newArry = new Array();
	for (var x in arry) {
		if (arry[x].pid == id){
			newArry.push(arry[x]);
		}
	}

	return newArry;
}
function updateUsePwd(){
	layer.open({
		type:2,
		title: "修改密码",
		fixed:false,
		resize :false,
		shadeClose: true,
		area: ['700px','500px'],
		content:'../member/password.html'
	});
}

